package com.wy.statusbar;


import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * Created by zhy on 15/9/21.
 */
public class StatusBarUtil {


    /**
     * 为我们的状态栏设置颜色
     */
    public static void setStatusBarColor(Activity activity, int color) {

        //5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(color);
        }
        //4.4-5.0之间，采用一个技巧，首先把它弄成全屏，在状态栏里面再加一个布局
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //首先把它弄成全屏
            //activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //在状态栏里面再加一个布局(高度是状态栏的高度)
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            View view = new View(activity);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)));
            view.setBackgroundColor(color);

            //android:fitsSystemWindows="true",每个布局都要写

            decorView.addView(view, 0);

            //android:fitsSystemWindows="true",每个布局都要写，方法1：设置一个padding
            ViewGroup contentView = activity.findViewById(android.R.id.content);
//            contentView.setPadding(0, getStatusBarHeight(activity), 0, 0);

            //android:fitsSystemWindows="true",每个布局都要写，方法1：找到Activity根布局设置fitsSystemWindows
            ViewGroup activityView = (ViewGroup) contentView.getChildAt(0);
            activityView.setFitsSystemWindows(true);

        }

    }

    /**
     * 获取状态栏高度
     */
    private static int getStatusBarHeight(Activity activity) {
        Resources resources = activity.getResources();
        int statusBarHeightId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelOffset(statusBarHeightId);
    }

    /**
     * 设置Activity全屏
     */
    public static void setActivityTranslucent(Activity activity) {

        //5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        //4.4-5.0之间，采用一个技巧，首先把它弄成全屏，在状态栏里面再加一个布局
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

    }

}